﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.foreman
{
    public partial class foreman_edit : Web.UI.ManagePage
    {
        string defaultpassword = "0|0|0|0"; //默认显示密码
        protected string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.foreman_user().Exists(this.id))
                {
                    JscriptMsg("信息不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("foreman_list", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.users bllUsers = new BLL.users();
            BLL.foreman_user bllForemanUser = new BLL.foreman_user();
            var model = bllForemanUser.GetModel(_id);
            model.user = bllUsers.GetModel(model.user_id);

            txtNickName.Text = model.user.nick_name;
            txtAvatar.Text = model.user.avatar;
            txtMobile.Text = model.user.mobile;
            rblForeWork.SelectedValue = model.fore_work.ToString();
            txtAddress.Text = model.user.address;
            txtForeSettime.Text = model.fore_settime.ToString("yyyy-MM-dd");
            txtForeAreas.Text = model.fore_areas;
            txtForeInfo.InnerText = model.fore_info;
            txtForeExp.Text = model.fore_exp;
            rblRenZheng.SelectedValue = model.fore_renzheng.ToString();
            rblForeLevel.SelectedValue = model.fore_level.ToString();
            rblForeGrade.SelectedValue = model.fore_grade;

            if (model.fore_safe.Length > 1)
            {
                var arr=model.fore_safe.Split(',');
                foreach (var item in arr)
                {
                   var curItem= cblForeSafe.Items.FindByValue(item);
                    if(curItem!=null)curItem.Selected = true;
                }
            }

            rblForeIsRed.SelectedValue = model.fore_isred.ToString();
            rblForeIsTop.SelectedValue = model.fore_istop.ToString();
            txtForeSort.Text = model.fore_sort.ToString();
            txtForeFrom.Text = model.fore_from;
            rblStatus.SelectedValue = model.user.status.ToString();

        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            bool result = false;
            Model.foreman_user model = new Model.foreman_user();
            model.user = new Model.users();

            BLL.users bllUsers = new BLL.users();
            BLL.foreman_user bllForeman = new BLL.foreman_user();

            model.user.nick_name = Utils.DropHTML(txtNickName.Text);
            model.user.avatar = Utils.DropHTML(txtAvatar.Text);
           model.user.mobile = Utils.DropHTML(txtMobile.Text);
            model.fore_work = int.Parse(rblForeWork.SelectedValue);
            model.user.address = Utils.DropHTML(txtAddress.Text);
            model.fore_settime = DateTime.Parse(txtForeSettime.Text);
            model.fore_areas = Utils.DropHTML(txtForeAreas.Text);
            model.fore_info = Utils.DropHTML(txtForeInfo.InnerText);
            model.fore_exp = Utils.DropHTML(txtForeExp.Text);
            model.fore_renzheng = int.Parse(rblRenZheng.SelectedValue);
            model.fore_level = int.Parse(rblForeLevel.SelectedValue);
            model.fore_grade = Utils.DropHTML(rblForeGrade.SelectedValue);

            model.fore_safe ="";
            foreach (ListItem item in cblForeSafe.Items)
            {
                if (item.Selected == true)
                    model.fore_safe += item.Value + ",";
            }
            model.fore_safe = model.fore_safe.TrimEnd(',');
            
            model.fore_isred = int.Parse(rblForeIsRed.SelectedValue);
            model.fore_istop = int.Parse(rblForeIsTop.SelectedValue);
            model.fore_sort = int.Parse(txtForeSort.Text);
            model.fore_from = Utils.DropHTML(txtForeFrom.Text);
            model.user.status = int.Parse(rblStatus.SelectedValue);

            model.user.user_name = model.user.mobile;
            model.user.salt = Utils.GetCheckCode(6); //获得6位的salt加密字符串
            model.user.password = DESEncrypt.Encrypt(model.user.mobile, model.user.salt);//以随机生成的6位字符串做为密钥加密

            model.user_id = bllUsers.Add(model.user);

            if (model.user_id>0 && bllForeman.Add(model)>0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加工长:" + model.user.nick_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.users bllUsers = new BLL.users();
            BLL.foreman_user bllForeman = new BLL.foreman_user();

            Model.foreman_user model = bllForeman.GetModel(_id);
            model.user = bllUsers.GetModel(model.user_id);

            model.user.nick_name = Utils.DropHTML(txtNickName.Text);
            model.user.avatar = Utils.DropHTML(txtAvatar.Text);
            model.user.mobile = Utils.DropHTML(txtMobile.Text);
            model.fore_work = int.Parse(rblForeWork.SelectedValue);
            model.user.address = Utils.DropHTML(txtAddress.Text);
            model.fore_settime = DateTime.Parse(txtForeSettime.Text);
            model.fore_areas = Utils.DropHTML(txtForeAreas.Text);
            model.fore_info = Utils.DropHTML(txtForeInfo.InnerText);
            model.fore_exp = Utils.DropHTML(txtForeExp.Text);
            model.fore_renzheng = int.Parse(rblRenZheng.SelectedValue);
            model.fore_level = int.Parse(rblForeLevel.SelectedValue);
            model.fore_grade = Utils.DropHTML(rblForeGrade.SelectedValue);

            model.fore_safe = "";
            foreach (ListItem item in cblForeSafe.Items)
            {
                if (item.Selected == true)
                    model.fore_safe += item.Value + ",";
            }
            model.fore_safe = model.fore_safe.TrimEnd(',');

            model.fore_isred = int.Parse(rblForeIsRed.SelectedValue);
            model.fore_istop = int.Parse(rblForeIsTop.SelectedValue);
            model.fore_sort = int.Parse(txtForeSort.Text);
            model.fore_from = Utils.DropHTML(txtForeFrom.Text);
            model.user.status = int.Parse(rblStatus.SelectedValue);

            if (bllUsers.Update(model.user) && bllForeman.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "修改工长信息:" + model.user.nick_name); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("foreman_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改工长成功！", "foreman_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("foreman_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加工长成功！", "foreman_list.aspx");
            }
        }

    }
}